package com.itheima.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.mapper.FavoriteMapper;
import com.itheima.travel.mapper.RouteMapper;
import com.itheima.travel.mapperExt.FavoriteMapperExt;
import com.itheima.travel.mapperExt.RouteMapperExt;
import com.itheima.travel.pojo.Favorite;
import com.itheima.travel.pojo.FavoriteExample;
import com.itheima.travel.pojo.Route;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.req.UserVo;
import com.itheima.travel.service.FavoriteService;
import com.itheima.travel.utils.BeanConv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private HttpSession session;


    //声明自定义mapper
    @Autowired
    private FavoriteMapperExt favoriteMapperExt;

    @Autowired
    private RouteMapperExt routeMapperExt;

    @Autowired
    private RouteMapper routeMapper;

    @Override
    public boolean isFavorited(Long routeId) {

        //从session中获取用户信息
        UserVo userVo = (UserVo) session.getAttribute(session.getId());
        //根据用户的id和旅游线路的id从数据库的中间表tab_favorite查询

        //组装查询条件
        FavoriteExample favoriteExample = new FavoriteExample();
        favoriteExample.createCriteria()
                .andUserIdEqualTo(userVo.getId())
                .andRouteIdEqualTo(routeId);

        List<Favorite> favorites = favoriteMapper.selectByExample(favoriteExample);


        return favorites.size()==1;
    }

    @Override
    public PageInfo<RouteVo> findMyFavorite(int pageNum, int pageSize) {

        //通知mybatis进行分页查询
        PageHelper.startPage(pageNum,pageSize);

        //获取当前登录的用户的id
        UserVo userVo = (UserVo) session.getAttribute(session.getId());
        //1.分页页面需要的当前用户收藏的旅游线路信息
        //2.当前用户收藏的旅游线路信息的总数量
        /**
         *  此处的routeList是什么类型？
         *  是Page类型，page继承了ArrayList,并且page拥有自己的属性：total
         */
        List<Route> routeList = favoriteMapperExt.findMyFavoriteByPage(userVo.getId());

        //pageInfo就是pageBean对象
        /**
         *  将routeList(Page类型)里面的list和total取出来了，
         *  赋值给了pageInfo的list和total属性
         */
        PageInfo<Route> pageInfo = new PageInfo<>(routeList);


        //属性拷贝
        PageInfo<RouteVo> voPageInfo = new PageInfo<>();
        //先做total属性的拷贝
        BeanConv.toBean(pageInfo,voPageInfo);

        //再做list集合的单独拷贝
        List<RouteVo> routeVoList = BeanConv.toBeanList(pageInfo.getList(), RouteVo.class);
        voPageInfo.setList(routeVoList);



      /*  System.out.println("分页数据：");
        pageInfo.getList().forEach(route -> {
            System.out.println(route);
        });
        System.out.println("分页总数量："+pageInfo.getTotal());*/

      return voPageInfo;
    }

    @Override
    @Transactional //添加事务
    public int addFavorite(Long routeId) {

        //1.向中间表添加一条数据
        //从session中获取用户信息
        UserVo userVo = (UserVo) session.getAttribute(session.getId());

        Favorite favorite = Favorite.builder()
                .userId(userVo.getId())
                .routeId(routeId)
                .build();

        favoriteMapper.insert(favorite);
        //2.将route表的attention_count字段值加1
        int i = 1/0;
        routeMapperExt.updateAttentionCountByPrimaryKey(routeId);
        //3.重新查询route表的attention_cout字段值
        Route route = routeMapper.selectByPrimaryKey(routeId);

        return route.getAttentionCount();
    }
}
